package cn.b.sky.shop.storage;

import lombok.Data;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lidapeng
 * @Description: 对象的基本信息(元数据)
 * @Date Created in 8/18/23 2:49 PM
 * @Modified By: lidapeng
 * Copyright(c) dbappsecurity.com.cn
 */
@Data
public class CloudObjectMetadata implements Comparable<CloudObjectMetadata> {

    private Long contentLength;
    private String path;
    private Date createTime;
    private Date lastModified;
    private boolean broken;
    private String creatorName;
    private Long creatorId;
    private Long sessionId;
    private String md5sum;
    private Map<String, String> userHeaders;
    private Map<String, Object> headers;
    private Object originalMetadata;

    public CloudObjectMetadata(String path) {
        this.contentLength = 0L;
        this.broken = false;
        this.userHeaders = new HashMap(0);
        this.headers = new HashMap(0);
        this.path = path;
    }

    public CloudObjectMetadata(String path, Date date, Long contentLength, Object originalMetadata) {
        this(path, date, contentLength);
        this.originalMetadata = originalMetadata;
    }

    public CloudObjectMetadata(String path, Date date, Long contentLength) {
        this.contentLength = 0L;
        this.broken = false;
        this.userHeaders = new HashMap(0);
        this.headers = new HashMap(0);
        this.lastModified = date;
        this.contentLength = contentLength;
        this.path = path;
    }

    public CloudObjectMetadata addUserHeader(String key, String value) {
        this.userHeaders.put(key, value);
        return this;
    }

    public CloudObjectMetadata addHeaders(String key, String value) {
        this.headers.put(key, value);
        return this;
    }

    public int compareTo(CloudObjectMetadata o) {
        String name1 = this.getPath();
        String name2 = o.getPath();
        if (name1 == null) {
            return -1;
        } else if (name2 == null) {
            return 1;
        } else {
            boolean isDir1 = name1.charAt(name1.length() - 1) == '/';
            boolean isDir2 = name2.charAt(name2.length() - 1) == '/';
            if (!isDir1 && !isDir2) {
                return this.getLastModified() != null && o.getLastModified() != null ? o.getLastModified().compareTo(this.getLastModified()) : name2.compareTo(name2);
            } else if (isDir1 && isDir2) {
                return name2.compareTo(name2);
            } else {
                return isDir1 ? -1 : 1;
            }
        }
    }
}
